export const setStore = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
	content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return
  return window.localStorage.getItem(name)
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return
  window.localStorage.removeItem(name)
}

/**
 * 页面到达底部，加载更多
 */
export const loadMore = (element, callback) => {
  let windowHeight = window.screen.height
  let height
  let setTop
  let paddingBottom
  let marginBottom
  let requestFram
  let oldScrollTop
  document.body.addEventListener('scroll', () => {
    loadMore()
  }, false)
    //运动开始时获取元素 高度 和 offseTop, pading, margin
  element.addEventListener('touchstart', () => {
    height = element.offsetHeight
    setTop = element.offsetTop
    paddingBottom = getStyle(element,'paddingBottom')
    marginBottom = getStyle(element,'marginBottom')
  }, {passive: true})

    //运动过程中保持监听 scrollTop 的值判断是否到达底部
  element.addEventListener('touchmove', () => {
    loadMore()
  }, {passive: true})

    //运动结束时判断是否有惯性运动，惯性运动结束判断是非到达底部
  element.addEventListener('touchend', () => {
    oldScrollTop = document.body.scrollTop
    moveEnd()
  }, {passive: true})
    
  const moveEnd = () => {
    requestFram = requestAnimationFrame(() => {
      if (document.body.scrollTop != oldScrollTop) {
        oldScrollTop = document.body.scrollTop
        loadMore()
        moveEnd()
      } else {
        cancelAnimationFrame(requestFram)
        //为了防止鼠标抬起时已经渲染好数据从而导致重获取数据，应该重新获取dom高度
        height = element.offsetHeight
        loadMore()
      }
    })
  }

  const loadMore = () => {
    if (document.body.scrollTop + windowHeight >= height + setTop + paddingBottom + marginBottom) {
      callback()
    }
  }
}

export const getStyle = (element, attr, NumberMode = 'int') => {
  let target
  // scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
  if (attr === 'scrollTop') { 
    target = element.scrollTop
  } else if (element.currentStyle) {
    target = element.currentStyle[attr]
  } else { 
    target = document.defaultView.getComputedStyle(element,null)[attr]
  }
  //在获取 opactiy 时需要获取小数 parseFloat
  return  NumberMode == 'float'? parseFloat(target) : parseInt(target)
} 

var S_LANG_JA = 'JA'
var S_LANG_EN = 'EN'
var S_LANG_ZH = 'ZH'
var S_LANG_NL = 'NL'
var S_LANG_FR = 'FR'
var S_LANG_DE = 'DE'
var S_LANG_HU = 'HU'
var S_LANG_IT = 'IT'
var S_LANG_KO = 'KO'
var S_LANG_PL = 'PL'
var S_LANG_PT = 'PT'
var S_LANG_RU = 'RU'
var S_LANG_ES = 'ES'
var S_LANG_TH = 'TH'
var S_LANG_VI = 'VI'

const galleryInfo = {
  S_LANGS: [
    S_LANG_EN,
    S_LANG_ZH,
    S_LANG_ES,
    S_LANG_KO,
    S_LANG_RU,
    S_LANG_FR,
    S_LANG_PT,
    S_LANG_TH,
    S_LANG_DE,
    S_LANG_IT,
    S_LANG_VI,
    S_LANG_PL,
    S_LANG_HU,
    S_LANG_NL
  ],
  REGS: ['[(\\[]eng(?:lish)?[)\\]]', '[(\uFF08\\[]ch(?:inese)?[)\uFF09\\]]|[汉漢]化|中[国國][语語]|中文', '[(\\[]spanish[)\\]]|[(\\[]Español[)\\]]', '[(\\[]korean?[)\\]]', '[(\\[]rus(?:sian)?[)\\]]', '[(\\[]fr(?:ench)?[)\\]]', '[(\\[]portuguese', '[(\\[]thai(?: ภาษาไทย)?[)\\]]|แปลไทย', '[(\\[]german[)\\]]', '[(\\[]italiano?[)\\]]', '[(\\[]vietnamese(?: Tiếng Việt)?[)\\]]', '[(\\[]polish[)\\]]', '[(\\[]hun(?:garian)?[)\\]]', '[(\\[]dutch[)\\]]']
}

export const generateSLang = (title) => {
  var simpleLanguage = null
  for (let i = 0; i < galleryInfo.S_LANGS.length; i++) {
    if (new RegExp(galleryInfo.REGS[i], 'gi').test(title)) {
      simpleLanguage = galleryInfo.S_LANGS[i]
    }
  }
  return simpleLanguage
}

export const formatDateTime = (inputTime) => {
  var date = new Date(inputTime)
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m
  var d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  var h = date.getHours()
  h = h < 10 ? ('0' + h) : h
  var minute = date.getMinutes()
  var second = date.getSeconds()
  minute = minute < 10 ? ('0' + minute) : minute
  second = second < 10 ? ('0' + second) : second
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
}
